<?php
/**
 * 玩家进入页面
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordguide{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 游戏文字描述
	 */
	function introducegame(){
		$uin = sessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (!empty($result)){
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			//获取今天的0点到24点
			$datetime = App_Overlord_GameStart::getTime();
			//计数
			$count = 0;
			if (empty($result['Value']['login_fate'])){
				$result['Value']['login_fate'] = array('count' => 1 , 'time' => $time , 'state' => 0);
				$count = 1;
			}else{
				//昨天的0点
				$yesterday = $datetime[0] - 24 * 3600;
				//如果不是昨天登录的，算第一天
				if ($result['Value']['login_fate']['time'] < $yesterday){
					$result['Value']['login_fate']['count'] = 1;
					$result['Value']['login_fate']['time'] = $time;
					$result['Value']['login_fate']['state'] = 0;
					$count = 1;
				}elseif ($yesterday <= $result['Value']['login_fate']['time'] && $datetime[0] > $result['Value']['login_fate']['time']){
					if ($result['Value']['login_fate']['count'] >= 7){
						$result['Value']['login_fate']['count'] = 1; //大于7天重新赋值为1天
					}else {
						$result['Value']['login_fate']['count'] += 1;
					}
					$result['Value']['login_fate']['time'] = $time;
					$result['Value']['login_fate']['state'] = 0;
					$count = 1;
				}
			}
			if (empty($result['Value']['name'])){
				loadLib('User');
				$baseinfo = User::getBaseInfo($uin);
				if (empty($baseinfo)){
					return View::error('获取信息失败!');
				}
				$result['Value']['name'] = $baseinfo['name'];
				$count = 1;
			}
			if (!empty($count)){
				$code = App_Overlord_Query::alterUser($uin , $result['Value']);
			}
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
		}
		$smarty = loadSmarty();
		return $smarty->fetch('app/Overlord/introducegame.tpl.php');
	}
	/**
	 * 进入游戏页面
	 * Enter description here ...
	 */
	function index(){
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$count = App_Overlord_GameStart::getCountUser();

		loadLib('Friend');
		$friend_list = Friend::get(SessionInfo('uin'));

		// 获取用户数据
		loadLib('User');
		$friend_count = 0;	
		if (!empty($friend_list)){
			$user_in_info = App_Overlord_Query::allUser($friend_list);
			$friend_count = count($user_in_info);
		}
		$smarty->assign('friend_count',$friend_count);
		$smarty->assign('count',$count['count']);
		return $smarty->fetch('app/Overlord/prepare.tpl.php');
	}
	/**
	 * 游戏介绍页面
	 * Enter description here ...
	 */
	function introduce(){
		$smarty = loadSmarty();
		return $smarty->fetch('app/Overlord/introduce.tpl.php');
	}
	/**
	 * 选择国家
	 * Enter description here ...
	 */
	function country(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : 'no';
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (!empty($result)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
		}
		$smarty->assign('type' , $type);
		return $smarty->fetch('app/Overlord/country.tpl.php');
	}
	/**
	 * 显示进入页面
	 */
	function start_type(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		if (empty($type)){
			return View::error('请选择类型.');
		}
		loadLib('App_Overlord_GameStart');
		$start_name = App_Overlord_GameStart::getBarracksType($type); 
		if (empty($start_name)){
			return View::error('没有此类型.');
		}
		$smarty = loadSmarty();
		$smarty->assign('type' , $type);
		$smarty->assign('special' , $start_name[3]['special']);
		return $smarty->fetch('app/Overlord/start_type.tpl.php');
	}
	/**
	 * 进入新手引导
	 */
	function guide(){
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$task = App_Overlord_Query::oneTask($uin);
		if (empty($task)){
			return View::error('数据异常!');
		}
		$guide_type = strlen($task['Value']['freshman']['value']);
		if ($guide_type == 0){
			$user_info = App_Overlord_Query::oneUser($uin);
			$start_name = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']); 
			if (empty($start_name)){
				return View::error('没有此类型.');
			}
			$smarty->assign('country' , $user_info['Value']['country']);
			$smarty->assign('special' , $start_name[3]['special']);
		}
	
		if ($guide_type == 2 || $guide_type == 3 || $guide_type == 4){
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			$rand = array_rand($generals_info['Value']);
			if (!empty($generals_info)){
				$smarty->assign('generals_info' , $generals_info['Value'][$rand]);
			}
		}
		
		$smarty->assign('guide_type' , $guide_type);
		return $smarty->fetch('app/Overlord/guide.tpl.php');
	}
	/**
	 * 进入引导介绍界面
	 */
	function guide_introduce(){
		$guide_type = isset($_GET['guide_type']) ? trim($_GET['guide_type']) : 'city';
		$count = array(100, 200, 500, 1000, 2000, 5000);
		$smarty = loadSmarty();
		$smarty->assign('count',$count);
		$smarty->assign('guide_type' , $guide_type);
		return $smarty->fetch('app/Overlord/guide_introduce.tpl.php');
	}
	/**
	 * 进入游戏
	 */
	function start(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : 'wu';
		if (empty($type)){
			return View::error('请选择类型.');
		}
		$uin = sessionInfo('uin');
		$key = 'user_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (!empty($result)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
			break;
		}else{
			if($type == 'shu' || $type == 'wei' || $type == 'wu'){
				
				$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
				loadLib('User');
				$baseinfo = User::getBaseInfo($uin);
				if (empty($baseinfo)){
					return View::error('获取信息失败!');
				}
				/***********************初始化人物***********************/
				$value = array('country' => $type , 'name' => $baseinfo['name'] ,'lv' => 1 ,'exp' => 0 , 'gold' => 10000 , 'diamond' => 1000 , 'exploits' => 5 , 'time' => $time , 'new' => array('num' => 0, 'time' => $time) , 'task' => 0 , 'gift_lv' => 3 , 'login_fate' => array('count' => 1 , 'time' => $time , 'state' => 0));
				$code = App_Overlord_Query::addUser($uin, $value);
				
				if ($code == 0){
					$is_key = 'fight_' .$uin;
					$is_value = array('count' => 20, 'time' => $time);
					//初始化战斗次数
					$code = App_Overlord_Query::addCollocate('app_overlord_fight_count', $is_key, $is_value);
					//初始化城池
					$city = array('hkee' =>array('lv' => 1 , 'state' => 0 , 'time' => $time , 'to_uin' => 0 , 'generals' => 0),'assist' =>array(array('lv' => 1 , 'state' => 0 , 'time' => $time , 'to_uin' => 0 , 'generals' => 0))) ;
					$code = App_Overlord_Query::addCity($uin, $city);
					//初始化任务
					loadLib('App_Overlord_Task');
					//查询任务列表数据
					$task_info = App_Overlord_Query::oneTask($uin);
					if (empty($task_info)){
						$value = array(
							'freshman' =>array('value' => '', 'state' => array())
							);
						App_Overlord_Query::addTask($uin, $value);
					}
					//查询是否有材料建筑
					$build_info = App_Overlord_Query::oneCollocate('app_overlord_build', 'build_' . $uin);
					
					//物品配置类
					loadLib('App_Overlord_Collocate');
					//材料建筑配置
					$data = App_Overlord_Collocate::getMaterial();
					if (empty($build_info)){
						//初始化材料建筑
						$value = array('lv' => 1, 'time' => $time);
						App_Overlord_Query::addCollocate('app_overlord_build', 'build_' . $uin, $value);
					}
				// =================================== 查询兵营 ====================================
				$barracks_info = App_Overlord_Query::oneSoldiers($uin);
				//初始兵营
				if (empty($barracks_info)){
					$value = array(array('type' => 'infantry', 'count' => 500), array('type' => 'cavalryman', 'count' => 500), array('type' => 'toxotae', 'count' => 500), array('type' => 'special', 'count' => 500));
					App_Overlord_Query::addSoldiers($uin , $value);
				}
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide&type='.$type);
				}else{
					return View::error('进入游戏失败.');
				}
			}else{
				return View::error('请选择类型.');
			}
		}
	}
}

# end of script